package com.liqi.display.service.impl;

import com.liqi.display.common.SystemCode;
import com.liqi.display.entity.TblAnnounceNumber;
import com.liqi.display.mapper.TblAnnounceNumberMapper;
import com.liqi.display.service.ITblAnnounceNumberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liqi
 * @since 2021-04-16
 */
@Service
public class TblAnnounceNumberServiceImpl extends ServiceImpl<TblAnnounceNumberMapper, TblAnnounceNumber> implements ITblAnnounceNumberService {

    @Autowired
    private TblAnnounceNumberMapper announceNumberMapper;

    @Override
    public long getList(Date date) {
        //获取取号时间和叫号时间
        List<TblAnnounceNumber> list = announceNumberMapper.getList(date);
        long callTime = 0;
        long takeTime = 0;
        int size = 0;

        if(!CollectionUtils.isEmpty(list)) {
            for(int i=0; i<list.size(); i++) {
                Map<Object,Object> map = (Map<Object,Object>)list.get(i);
                if(map.get("CALL_TIME")!=null&&map.get("TAKE_TIME")!=null) {
                    Date ct = (Date)map.get("CALL_TIME");
                    Date td = (Date)map.get("TAKE_TIME");
                    callTime +=ct.getTime();//叫号毫秒数累加
                    takeTime +=td.getTime();//取号毫秒数累加
                    size = size +1;//记录数
                }
            }
            System.out.println("callTime:"+callTime);
            System.out.println("takeTime:"+takeTime);
            //叫号时间大于取号时间
            if(callTime>takeTime) {
                long sub = callTime - takeTime;//叫号时间-取号时间
                long num = sub / size;//取整
                if(num>size) {//毫秒数大于记录数
                    System.out.println("时间的毫秒数:"+num);
                    long minutes = TimeUnit.MILLISECONDS.toMinutes(num);//毫秒数转分钟
                    System.out.println("时间的分钟数:"+minutes);
                    return minutes;
                }
            }
        }
        return 5;
    }

    /**
     * 获取平均等待时间
     * @param date
     * @return
     */
	@Override
	public double getWaitTime(Date date) {
		/*select sum(cost_s)/sum(b.total)/60
		from (
		select substr(t.take_code,2,1) business_type--一级专区编码
		,count(1)total--取号叫号总数
		,sum((a.call_time-t.take_time)*86400) as cost_s--等待时间总数
		from tbl_take_number t, TBL_ANNOUNCE_NUMBER a
		where a.take_code is not null 
		and to_char(t.take_time,'yyyymmdd')||t.take_code=to_char(a.call_time,'yyyymmdd')||a.take_code
		and t.take_time>=to_date('2021/08/16','yyyy/mm/dd') and substr(t.take_code,2,1) not in ('E','F')
		group by substr(t.take_code,2,1)
		) b*/
		return announceNumberMapper.getWaitTime(date);
	}
    
    
    
}
